package com.zs.leymovie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;
import com.zs.leymovie.adapter.MoiveDetailAdapter;
import com.zs.leymovie.adapter.MovieDetailLvAdapter;
import com.zs.leymovie.bean.Comment;
import com.zs.leymovie.bean.CommentDataEntity;
import com.zs.leymovie.bean.CommentEntity;
import com.zs.leymovie.bean.DetailDataEntity;
import com.zs.leymovie.bean.DetailMovieDataEntity;
import com.zs.leymovie.bean.MovieComing;
import com.zs.leymovie.bean.MovieDetail;
import com.zs.leymovie.bean.MovieShowing;
import com.zs.leymovie.constants.UrlConstants;
import com.zs.leymovie.utils.ShareHelper;

import java.util.ArrayList;


@ContentView(R.layout.activity_movie_detail)
public class MovieDetailActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2,View.OnClickListener {
    @ViewInject(R.id.movie_detail_back)
    private TextView back;
    @ViewInject(R.id.movie_detail_share)
    private ImageView share;

    @ViewInject(R.id.movie_detail_scrollview)
    private PullToRefreshScrollView mScrollView;

    private BitmapUtils bitmapUtils = null;
    private MoiveDetailAdapter scrollAdapter;
    private ArrayList<Comment>  comments = new ArrayList<>();
    private MovieDetailLvAdapter detailLvAdapter;
    private DetailMovieDataEntity movieDataEntity = null;
    private String movie_id ;
    private String city_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initView();
        Intent intent = getIntent();
        initData(intent);
    }

    private void initData(Intent intent) {
        String movieInfoUrl = null;
        String commentUrl = null;
        String flag = intent.getStringExtra("flag");
        city_id = intent.getStringExtra("city_id");
        if (TextUtils.equals(flag, "Moving")) {
            MovieShowing ms = (MovieShowing) intent.getSerializableExtra("movie");
            back.setText(ms.getMovie_name());
            movie_id = ms.getMovie_id();
            movieInfoUrl = UrlConstants.getShowingInfoUrl(city_id,movie_id);
            commentUrl = UrlConstants.getComment(ms.getMovie_id(),city_id);
        } else if (TextUtils.equals(flag, "Coming")) {
            MovieComing ms = (MovieComing) intent.getSerializableExtra("movie");
            back.setText(ms.getMovie_name());
            movie_id = ms.getMovie_id();
            movieInfoUrl = UrlConstants.getShowingInfoUrl(city_id,movie_id);
            commentUrl = UrlConstants.getComment(ms.getMovie_id(),city_id);
        }else if(TextUtils.equals(flag,"yyMoving")){
            String movename = intent.getStringExtra("moveName");
            String moveid = intent.getStringExtra("moveId");
            String cityid = intent.getStringExtra("cityId");
            back.setText(movename);  //设置城市名字
            movieInfoUrl = UrlConstants.getShowingInfoUrl(cityid,moveid);
            commentUrl = UrlConstants.getComment(moveid,cityid);
        }

        //加载影片信息
        getMovieInfo(movieInfoUrl);

        //加载评论写信
        getCommentInfo(commentUrl);

    }

    //获取评论
    private void getCommentInfo(String commentUrl) {
        HttpUtils utils = new HttpUtils();
        if(commentUrl != null && commentUrl.length() > 0){
            utils.send(HttpRequest.HttpMethod.GET, commentUrl, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    String info = responseInfo.result;
                    CommentEntity  entity = JSON.parseObject(info, CommentEntity.class);
                    if(entity.getStatus() == 1){
                        CommentDataEntity dataEntity = entity.getData();
                        comments.addAll(dataEntity.getComments());
                        detailLvAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    UrlConstants.getPrompt();
                }
            });
        }

    }

    //获取电影信息
    private void getMovieInfo(String movieInfoUrl) {
        HttpUtils utils = new HttpUtils();
        if(movieInfoUrl != null && movieInfoUrl.length() > 0){
            utils.send(HttpRequest.HttpMethod.GET, movieInfoUrl, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    String info = responseInfo.result;
                    MovieDetail movieDetail = JSON.parseObject(info, MovieDetail.class);
                    if(movieDetail.getStatus() == 1){
                        DetailDataEntity dataEntity = movieDetail.getData();
                        movieDataEntity = dataEntity.getMovie_data();
                        scrollAdapter.getItemView(movieDataEntity);
                    }
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    UrlConstants.getPrompt();
                }
            });
        }
    }

    private void initView() {
        share.setOnClickListener(this);
        View view = LayoutInflater.from(this).inflate(R.layout.detail_lv_head_view,null);
        mScrollView.addView(view);
        scrollAdapter = new MoiveDetailAdapter(MovieDetailActivity.this,view);
        detailLvAdapter = new MovieDetailLvAdapter(comments,this);
        scrollAdapter.vh.mListView.setAdapter(detailLvAdapter);

    }

    public String getCity_id() {
        return city_id;
    }

    public void setCity_id(String city_id) {
        this.city_id = city_id;
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.movie_detail_share:
                StringBuffer buffer = new StringBuffer();
                buffer.append("标题 ："+movieDataEntity.getMovie_name()+"  , ");
                buffer.append("上映时间 ："+movieDataEntity.getMovie_show_date()+"  , ");
                buffer.append("类型 ："+movieDataEntity.getMovie_type()+"  , ");
                buffer.append("片长 ："+movieDataEntity.getMovie_length()+"分钟, ");
                buffer.append("导演 ："+movieDataEntity.getMovie_director()+"  , ");
                buffer.append("主演 ："+movieDataEntity.getMovie_cast()+"  , ");
                buffer.append("剧情简介 ："+movieDataEntity.getMovie_short_desc()+"  , ");
                buffer.append("好片抢先看 ："+movieDataEntity.getMovie_trailer_url());
                String buff  = buffer.toString();
                if(buff.length() > 140){
                    buff = buff.substring(0,120);
                }
                ShareHelper.showShare(this,buff);
                break;
        }
    }

    public String getMovie_id() {
        return movie_id;
    }

    public void setMovie_id(String movie_id) {
        this.movie_id = movie_id;
    }
}
